package com.example.orderapplication.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.example.orderapplication.R
import com.example.orderapplication.bean.Data
import com.example.orderapplication.bean.DataX
import com.example.orderapplication.view.GridViewForScrollView


/**
 * 右侧主界面ListView的适配器
 *
 * @author Administrator
 */
class HomeAdapter(private val context: Context, foodDatas: List<Data>?) :
    BaseAdapter() {
    private val foodDatas: List<Data>? = foodDatas
    override fun getCount(): Int {
        return foodDatas?.size ?: 10
    }

    override fun getItem(position: Int): Any {
        return foodDatas!!.size
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var convertView = convertView
        val dataBean: Data = foodDatas!![position]
        val dataList: List<DataX>? = dataBean.dataList
        var viewHold: ViewHold?
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_home, null)
            viewHold = ViewHold()
            viewHold.gridView =
                convertView.findViewById<View>(R.id.gridView) as GridViewForScrollView
            viewHold.blank = convertView.findViewById<View>(R.id.blank) as TextView
            convertView.tag = viewHold
        } else {
            viewHold = convertView.tag as ViewHold
        }
        val adapter = HomeItemAdapter(context, dataList)
        viewHold.blank?.text = dataBean.moduleTitle
        viewHold.gridView?.adapter = adapter
        adapter.setOnInnerItemOnClickListener(object : HomeItemAdapter.InnerItemOnclickListener {
            override fun itemClick(item: Int) {
                mListener?.itemClick(item)
            }

        })
        adapter.setLLOnInnerItemOnClickListener(object :
            HomeItemAdapter.LLInnerItemOnclickListener {
            override fun itemClick(item: DataX) {
                mDetailsListener?.DetailsitemClick(item)
            }

        })
        return convertView!!
    }

    private class ViewHold {
        var gridView: GridViewForScrollView? = null
        var blank: TextView? = null
    }

    var mListener: InnerItemOnclickListener? = null

    interface InnerItemOnclickListener {
        fun itemClick(item: Int)
    }

    fun setOnInnerItemOnClickListener(listener: InnerItemOnclickListener) {
        mListener = listener
    }


    var mDetailsListener: DetailsInnerItemOnclickListener? = null

    interface DetailsInnerItemOnclickListener {
        fun DetailsitemClick(item: DataX)
    }

    fun setDetailsOnInnerItemOnClickListener(listener: DetailsInnerItemOnclickListener) {
        mDetailsListener = listener
    }
}
